package com.kye.express.business.order.remark;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;

import java.util.List;

import ikidou.reflect.TypeToken;
import io.reactivex.Observable;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface RemarkContract {
    interface RemarkView extends IView {

        void onRemarks(List<String> remarks);

        void onFinish(String remark);
    }

    class RemarkPresenter extends BasePresenter<RemarkView> {

        public void getRemarks() {
            Observable<List<String>> forRx = SharedPreferencesManager.getForRx(SharedPreferencesConfig.KEY_REMARKS, new TypeToken<List<String>>() {
            }.getType());
            forRx
                    .compose(getView().<List<String>>bindToLifecycle())
                    .subscribe(new Consumer<List<String>>() {
                        @Override
                        public void accept(List<String> remarks) throws Exception {
                            getView().onRemarks(remarks);
                        }
                    });
        }

        public void saveRemark(String remark) {
            Observable<List<String>> forRx = SharedPreferencesManager.getForRx(SharedPreferencesConfig.KEY_REMARKS, new TypeToken<List<String>>() {
            }.getType());

            forRx
                    .map(new Function<List<String>, List<String>>() {
                        @Override
                        public List<String> apply(List<String> remarks) throws Exception {
                            while (remarks.size() >= 5) remarks.remove(0);
                            return remarks;
                        }
                    })
                    .firstOrError()
                    .map(new Function<List<String>, List<String>>() {
                        @Override
                        public List<String> apply(List<String> remarks) throws Exception {
                            remarks.remove(remark);
                            remarks.add(remark);
                            return remarks;
                        }
                    })
                    .flatMap(new Function<List<String>, SingleSource<Boolean>>() {
                        @Override
                        public SingleSource<Boolean> apply(List<String> remarks) throws Exception {
                            return SharedPreferencesManager.commitForRx(SharedPreferencesConfig.KEY_REMARKS, remarks);
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BiConsumer<Boolean, Throwable>() {
                        @Override
                        public void accept(Boolean aBoolean, Throwable throwable) throws Exception {
                            getView().onFinish(remark);
                        }
                    });
        }
    }
}
